package 知识点复习.异步回调.异步任务的合并执行;

import java.util.concurrent.CompletableFuture;

//allOF()会等待所有的任务结束，以合并所有的任务
public class AllOfDemo {
    public static void main(String[] args) {
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName() + ":模拟异步任务1"));
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName() + ":模拟异步任务2"));
        CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName() + ":模拟异步任务3"));
        CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> System.out.println(Thread.currentThread().getName() + ":模拟异步任务4"));

        CompletableFuture<Void> all = CompletableFuture.allOf(future1, future2, future3, future4);
        all.join();
        System.out.println(Thread.currentThread().getName() + ":所有异步任务都已经执行完毕");
    }
}
